package com.JFapp.service.impl;

import com.JFapp.constants.SystemConstants;
import com.JFapp.domain.dto.LinkDto;
import com.JFapp.domain.entity.Link;
import com.JFapp.domain.entity.ResponseResult;
import com.JFapp.domain.vo.LinkVo;
import com.JFapp.domain.vo.PageVo;
import com.JFapp.mapper.LinkMapper;
import com.JFapp.service.LinkService;
import com.JFapp.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Link)表服务实现类
 *
 * @author makejava
 * @since 2024-03-13 21:46:43
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {


    @Override
    public ResponseResult getAllLink() {
        //查询所有审核通过的友链
        LambdaQueryWrapper<Link> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL)
                .eq(Link::getDelFlag, SystemConstants.FLAG);
        List<Link> list = list(lambdaQueryWrapper);
        //转换vo
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(list, LinkVo.class);

        return ResponseResult.okResult(linkVos);
    }

    @Override
    public ResponseResult addLink(LinkDto linkDto) {
        if (linkDto.getLinkDtoList() != null) {
            List<Link> links = BeanCopyUtils.copyBeanList(linkDto.getLinkDtoList(), Link.class);
            saveBatch(links, SystemConstants.Max_LINK_UPLOAD);
        } else {
            Link link = BeanCopyUtils.copyBean(linkDto, Link.class);
            save(link);
        }

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getAllLinkInAdmin(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Link> wrapper = queryWrapper.select().orderByAsc(Link::getStatus);
        Page page = new Page<>(pageNum, pageSize);
        page(page, wrapper);
        List<Link> records = page.getRecords();
        List<LinkVo> linkVo = BeanCopyUtils.copyBeanList(records, LinkVo.class);
        PageVo pageVo = new PageVo(linkVo, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getOneLinkById(Long id) {
        Link link = baseMapper.selectById(id);
        return ResponseResult.okResult(link);
    }

    @Override
    public ResponseResult updateLink(LinkVo linkVo) {
        LambdaUpdateWrapper<Link> updateWrapper = new LambdaUpdateWrapper<>();
        LambdaUpdateWrapper<Link> wrapper = updateWrapper.eq(Link::getId, linkVo.getId());
        Link link = BeanCopyUtils.copyBean(linkVo, Link.class);
        update(link, wrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteLinkById(Long id) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Link> wrapper = queryWrapper.select().eq(Link::getDelFlag, SystemConstants.DEL_FLAG);
        baseMapper.delete(wrapper);
        return ResponseResult.okResult();
    }

}
